<template>
  <el-dialog
    :title="{ add: '新增权限', edit: '编辑权限' }[mode]"
    :visible="show"
    @close="closeDialog"
  >
    <el-form ref="form" label-width="110px" :rules="rules" :model="form">
      <el-form-item prop="name" label="权限名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item prop="code" label="权限标识">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item prop="description" label="权限描述">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item prop="enVisible" label="企业可见">
        <el-switch
          v-model="form.enVisible"
          active-text="可见"
          inactive-text="不可见"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="btn">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/premission'
export default {
  model: { prop: 'show' },
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      mode: 'add',
      form: {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '0', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }],
        code: [{ required: true, message: '必填', trigger: 'change' }],
        description: [{ required: true, message: '必填', trigger: 'change' }],
        enVisible: [{ required: true, message: '必填', trigger: 'change' }],
        type: [{ required: true, message: '必填', trigger: 'change' }],
        pid: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 关闭按钮并清除残留数据
    closeDialog() {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '0', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      }
    },
    // 发送请求
    submit() {
      this.$refs.form.validate(async(v) => {
        if (v) {
          if (this.mode == 'add') {
            await sysPermissionPost(this.form)
          } else if (this.mode == 'edit') {
            await sysPermissionPut(this.form)
          }
          this.$message.success('成啦!')
          this.closeDialog()
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.btn {
  text-align: center;
}
</style>
